import Dep from './dep'
import { queueWatcher } from './scheduler'
let id = 0

class Watcher {
  constructor(vm, fn, cb, options) {
    this.vm = vm
    this.fn = fn
    this.cb = cb
    this.options = options
    this.id = id++
    this.getter = fn // fn就是页面渲染逻辑
    this.depsId = new Set()
    this.deps = []
    this.get()
  }

  get() {
    Dep.target = this
    this.getter() // 页面渲染逻辑
    Dep.target = null // 渲染结束
  }
  update() {
    queueWatcher(this)
    // this.get()
  }
  run() {
    this.get()
  }
  addDep(dep) {
    let id = dep.id
    if (!this.depsId.has(id)) {
      this.depsId.add(id)
      this.deps.push(dep)
      dep.addSub(this)
    }
  }
}

export default Watcher
